const step = 4; // 步长
const interval = 1; // 间隔
const startDate = "2023-01-01 00:00:00"; // 起始时间

const levelLowZUUU = 30; // 双流统计阈值1
const levelTopZUUU = 40; // 双流统计阈值2

const levelLowZUTF = 40; // 天府统计阈值1
const levelTopZUTF = 50; // 天府统计阈值2

const levelLowTMA = 60; // 终端统计阈值1
const levelTopTMA = 70; // 终端统计阈值2

const loader = require("./loader.js");
const dayjs = require("dayjs");

main();

function main() {
  loader.loadData().then((content) => handle(content));
}

function handle(content) {
  console.log(loader.stamp() + "开始计算...");
  const data = content.data;
  const tags = content.payloads.key.split(":");

  let endDate = "";
  let computedTime = new Date().getTime();

  let maxZUUU = 0;
  let maxZUTF = 0;
  let maxTMA = 0;

  let maxTimeZUUU = "";
  let maxTimeZUTF = "";
  let maxTimeTMA = "";

  let maxSectorZUUU = 0;
  let maxSectorZUTF = 0;
  let maxSectorTMA = 0;

  let maxIndexZUUU = 0;
  let maxIndexZUTF = 0;
  let maxIndexTMA = 0;

  let countLowZUUU = 0;
  let countTopZUUU = 0;
  let countLowZUTF = 0;
  let countTopZUTF = 0;
  let countLowTMA = 0;
  let countTopTMA = 0;
  let countALL = 0;
  let startDayjs = dayjs(startDate);
  for (let l = 0; l < data.length; l += interval) {
    if (startDayjs.isAfter(data[l].moment)) {
      continue;
    }
    let valZUUU = 0;
    let valZUTF = 0;
    let valTMA = 0;
    let valSector = 0;
    if (l + step < data.length) {
      endDate = data[l].moment;
      for (let k = 0; k < step; k++) {
        let i = l + k;

        for (let j = 0; j < tags.length; j++) {
          let val = data[i].content[j];
          let key = tags[j];
          if (key.includes("ZUUU_ARR")) {
            valZUUU += val;
            valTMA += val;
          }
          if (key.includes("ZUTF_ARR")) {
            valZUTF += val;
            valTMA += val;
          }
        }
        if (valSector < data[i].sector) {
          valSector = data[i].sector;
        }
      }
    }

    if (valZUUU > maxZUUU) {
      maxZUUU = valZUUU;
      maxTimeZUUU = data[l].moment;
      maxSectorZUUU = valSector;
      maxIndexZUUU = l;
    }
    if (valZUTF > maxZUTF) {
      maxZUTF = valZUTF;
      maxTimeZUTF = data[l].moment;
      maxSectorZUTF = valSector;
      maxIndexZUTF = l;
    }
    if (valTMA > maxTMA) {
      maxTMA = valTMA;
      maxTimeTMA = data[l].moment;
      maxSectorTMA = valSector;
      maxIndexTMA = l;
    }

    if (valZUUU >= levelLowZUUU) {
      countLowZUUU++;
    }
    if (valZUUU >= levelTopZUUU) {
      countTopZUUU++;
    }
    if (valZUTF >= levelLowZUTF) {
      countLowZUTF++;
    }
    if (valZUTF >= levelTopZUTF) {
      countTopZUTF++;
    }
    if (valTMA >= levelLowTMA) {
      countLowTMA++;
    }
    if (valTMA >= levelTopTMA) {
      countTopTMA++;
    }
    countALL++;
  }

  let rateLowZUUU = ((countLowZUUU / countALL) * 100).toFixed(2) + "%";
  let rateTopZUUU = ((countTopZUUU / countALL) * 100).toFixed(2) + "%";
  let rateLowZUTF = ((countLowZUTF / countALL) * 100).toFixed(2) + "%";
  let rateTopZUTF = ((countTopZUTF / countALL) * 100).toFixed(2) + "%";
  let rateLowTMA = ((countLowTMA / countALL) * 100).toFixed(2) + "%";
  let rateTopTMA = ((countTopTMA / countALL) * 100).toFixed(2) + "%";

  let message = `
计算结果:

ZUUU 最高架次: ${maxZUUU}, ${maxSectorZUUU} 扇区, ${maxTimeZUUU} 
ZUTF 最高架次: ${maxZUTF}, ${maxSectorZUTF} 扇区, ${maxTimeZUTF} 
TMA 最高架次: ${maxTMA}, ${maxSectorTMA} 扇区, ${maxTimeTMA}

ZUUU ≥ ${levelLowZUUU}架次: ${countLowZUUU} 小时, 占比 ${rateLowZUUU}
ZUUU ≥ ${levelTopZUUU}架次: ${countTopZUUU} 小时, 占比 ${rateTopZUUU}
ZUTF ≥ ${levelLowZUTF}架次: ${countLowZUTF} 小时, 占比 ${rateLowZUTF}
ZUTF ≥ ${levelTopZUTF}架次: ${countTopZUTF} 小时, 占比 ${rateTopZUTF}
TMA ≥ ${levelLowTMA}架次: ${countLowTMA} 小时, 占比 ${rateLowTMA}
TMA ≥ ${levelTopTMA}架次: ${countTopTMA} 小时, 占比 ${rateTopTMA}
`;

  let message2 = `
参数说明:

步长: ${step}
间隔: ${interval}
起始: ${startDate}
截止: ${endDate}
`;

  let message3 = `
高峰架次运行信息:

------ ZUUU 高峰 @${maxTimeZUUU} ------
周边活动: ${JSON.stringify(data[maxIndexZUUU].details.activity)}
运行方向: ${JSON.stringify(data[maxIndexZUUU].details.direction)}
扇区信息: ${JSON.stringify(data[maxIndexZUUU].details.seats)}
人员信息: ${JSON.stringify(data[maxIndexZUUU].details.users)}
带班信息: ${JSON.stringify(data[maxIndexZUUU].details.supervisors)}
流量详情: ${JSON.stringify(getFlow(tags, data[maxIndexZUUU].content))}

------ ZUTF 高峰 @${maxTimeZUTF} ------
周边活动: ${JSON.stringify(data[maxIndexZUTF].details.activity)}
运行方向: ${JSON.stringify(data[maxIndexZUTF].details.direction)}
扇区信息: ${JSON.stringify(data[maxIndexZUTF].details.seats)}
人员信息: ${JSON.stringify(data[maxIndexZUTF].details.users)}
带班信息: ${JSON.stringify(data[maxIndexZUTF].details.supervisors)}
流量详情: ${JSON.stringify(getFlow(tags, data[maxIndexZUTF].content))}

------ TMA 高峰 @${maxTimeTMA} ------
周边活动: ${JSON.stringify(data[maxIndexTMA].details.activity)}
运行方向: ${JSON.stringify(data[maxIndexTMA].details.direction)}
扇区信息: ${JSON.stringify(data[maxIndexTMA].details.seats)}
人员信息: ${JSON.stringify(data[maxIndexTMA].details.users)}
带班信息: ${JSON.stringify(data[maxIndexTMA].details.supervisors)}
流量详情: ${JSON.stringify(getFlow(tags, data[maxIndexTMA].content))}
`;
  computedTime = new Date().getTime() - computedTime;
  console.log(`${loader.stamp()}计算结束, 用时${computedTime}ms`);
  console.log(``);

  console.log("\x1b[33m%s\x1b[0m", message);
  console.log("\x1b[36m%s\x1b[0m", message2);
  console.log("\x1b[35m%s\x1b[0m", message3);
}

function getFlow(tags, data) {
  let res = {};
  for (var i = 0; i < tags.length; i++) {
    res[tags[i]] = data[i];
  }
  return res;
}
